package com.vim.modules.api.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vim.common.constants.DictConstants;
import com.vim.modules.api.model.Config;
import com.vim.modules.api.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @作者 Administrator
 * @时间 2019-09-19 9:39
 * @版本 1.0
 * @说明
 */
@RestController
@RequestMapping(value = "/configServer")
public class ConfigController {

    @Autowired
    private ConfigService configService;

    /**
     * 配置列表数据
     */
    @RequestMapping(value = "/dataList")
    public PageInfo<Config> dataList(@RequestParam(value = "pageNum") Integer pageNum,
                                     @RequestParam(value = "pageSize") Integer pageSize,
                                     @RequestBody Config config){
        PageHelper.startPage(pageNum, pageSize);
        return configService.findPageList(config);
    }

    /**
     * 配置数据
     */
    @RequestMapping(value = "/get")
    public Config get(@RequestParam(value = "id") String id){
        return configService.get(id);
    }

    /**
     * 更新数据
     */
    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    public ResponseEntity<Map<String, Object>> save(@RequestBody Config config){
        Map<String, Object> result = new HashMap<>();
        config.setStatus(DictConstants.CONFIG_SEND_STATUS.NOT_SEND.getValue());
        configService.saveOrUpdate(config);
        result.put("code", "SUCCESS");
        result.put("msg", "操作成功!");
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    /**
     * 推送配置
     */
    @RequestMapping(value = "/sendConfig", method = RequestMethod.POST)
    public ResponseEntity<Map<String, Object>> sendConfig(@RequestParam(value = "id") String id){
        Map<String, Object> result = new HashMap<>();
        Config config = configService.get(id);
        if(null != config){
            configService.sendConfig(config);
            result.put("code", "SUCCESS");
            result.put("msg", "操作成功!");
        }else{
            result.put("code", "FAIL");
            result.put("msg", "推送配置不存在!");
        }
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
}
